const app = getApp()
var checkResult = function (result) {
  if (!result || result.code !== 20000) {
    console.log('error' + result.code);
    wx.showToast({
      icon: 'none',
      title: '没有数据或者操作失败，请刷新尝试',
    });
    return false;
  }
  return true;
};

Page({
  data: {
    contest: {}
  },
  bindDateChange: function(e){
    console.log('fuck', e.detail.value);
    this.setData({
      holdDate: e.detail.value
    });
  },
  onLoad: function (options) {
    console.log('edit', options);
    var sid = options.sid;
    var that = this;
    if (sid) {
      this.setData({
        worktype: '编辑比赛'
      });
      wx.setNavigationBarTitle({
        title: this.data.worktype,
      });
      wx.cloud.callFunction({
        // 云函数名称
        name: 'admin',
        // 传给云函数的参数
        data: {
          action: 'contests',
          sid: parseInt(sid)
        },
        success: function (res) {
          console.log(res);
          var result = res.result;
          if (!result || result.code !== 20000) {
            console.log('error' + result.code);
            wx.showToast({
              icon: 'none',
              title: '操作失败，请刷新尝试',
            });
            return;
          }
          var contest = result.data[0];
          contest.weburl = app.globalData.webprefix + 'index.html?sid=' + contest.sid;
          var data = {};
          for(var key in contest){
            data[key] = contest[key];
          }
          that.setData(data);
        },
        fail: err => {
          console.log(err);
          wx.showToast({
            icon: 'none',
            title: '加载失败，请尝试刷新',
          })
        }
      });
      return;
    }
    this.setData({
      worktype: '新建比赛'
    });
    wx.setNavigationBarTitle({
      title: this.data.worktype,
    });
  },
  save: function (event) {
    console.log(this.data);
    if(!this.data.name){
      return;
    }
    var that = this;
    var holdDate = this.data.holdDate || (new Date());
    var data = {
      name: this.data.name,
      host: this.data.host,
      holdDate: new Date(holdDate),
      action: 'savecontest'
    };
    if(this.data._id){
      data.id = this.data._id;
    }
    wx.cloud.callFunction({
      // 云函数名称
      name: 'admin',
      // 传给云函数的参数
      data: data,
      success: function (res) {
        console.log(res);
        var result = res.result;
        if (!result || result.code !== 20400) {
          console.log('error' + result.code);
          wx.showToast({
            icon: 'none',
            title: '操作失败，请刷新尝试',
          });
          return;
        }
        wx.showToast({
          icon: 'none',
          title: '操作成功',
        });
        setTimeout(function(){
          wx.redirectTo({
            url: '../index/index',
          });
        }, 800);
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          icon: 'none',
          title: '加载失败，请尝试刷新',
        })
      }
    });
  }
})